package org.example;

import java.io.*;
import java.net.Socket;

public class FileSyncSocketClient {
    private static String SERVER_IP = "127.0.0.1";
    private static int SERVER_PORT = 8888;

   /* public FileSyncSocketClient(String ip,int port) {
        SERVER_IP = ip;
        SERVER_PORT = port;
    }*/

    public FileSyncSocketClient() {
    }

    public static void sendFileCreateRequest(File file) {
        sendRequest("CREATE", file.getAbsolutePath());
    }

    public static void sendFileUpdateRequest(File file) {
        sendRequest("UPDATE", file.getAbsolutePath());
    }

    public static void sendFileDeleteRequest(File file) {
        sendRequest("DELETE", file.getAbsolutePath());
    }

    private static void sendRequest(String action, String filePath) {
        try {
            // 连接服务器
            Socket socket = new Socket(SERVER_IP, SERVER_PORT);

            // 发送文件操作请求
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            writer.write(action + " " + filePath);
            writer.newLine();
            writer.flush();

            // 接收服务器响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String response = reader.readLine();
            System.out.println("服务器响应：" + response);

            // 关闭连接
            socket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
